// 类型切换模块        
var orderSwiper = new Swiper ('.options-box', {
    slideClass : 'options-li',
    direction: 'horizontal',
    loop: false,
    pagination: {
        el: '.model-box',
        clickable: true,
        bulletClass : 'model-li',
        bulletActiveClass: 'active',
        renderBullet: function (index,className) {
            var text, modelClass;
            switch(index){
                case 0:text='拼车';modelClass=' pooling-icon';break;  
                case 1:text='专车';modelClass=' special-icon';break;
            }
            return '<li class="'+ className + modelClass+'">'+ text +'</li>';
        },
    },
    on: {
        slideChangeTransitionStart: function(){
            var index = this.activeIndex;
            if(index == 0) $(".model-box").attr("class","model-box pooling-model");
            else $(".model-box").attr("class","model-box special-model");
        },
    },
}); 


// 订单模块
var carOrder = {

    schedule: "#uppage_schedule",
    shijian: "#uppage_shijian",

    // 提交拼车单
    submitPooling: function(){
        var start = $("#pooling_start").attr("siteid");
        var end = $("#pooling_end").attr("siteid");
        var date = $("#pooling_date").attr("date");
        var num = $("#pooling_num").find("option:selected").text();
        if(start == "") {
            tip.show("请选择出发地");
            return
        }
        if(end == "") {
            tip.show("请选择目的地");
            return
        }
        if(start == end) {
            tip.show("出发地与目的地不能一致",4000);
            return
        }
        // todo 提交信息，查询线路
        console.log(start,end,date,num);

        $(location).attr('href', "page/result.html");    
    },

    // 提交专车单
    submitSpecial: function(){
        var startName = $("#special_start").val();
        var startLng = $("#special_start").attr("lng");
        var startLat = $("#special_start").attr("lat");
        var startDistrict = $("#special_start").attr("district");
        var endName = $("#special_end").val();
        var endLng = $("#special_end").attr("lng");
        var endLat = $("#special_end").attr("lat");
        var endDistrict = $("#special_end").attr("district");
        var date = $("#special_date").val();
        var people = $("#special_people").val();
        var phone = $("#special_phone").val();
        if(startName == "") {
            tip.show("请输入出发地");
            return
        }
        if(endName == "") {
            tip.show("请输入目的地");
            return
        }
        if(startDistrict == endDistrict) {
            tip.show("出发地与目的地不能一致",4000);
            return
        }
        if(people == "") {
            tip.show("请输入联系人");
            return
        }
        if(phone == "") {
            tip.show("请输入联系电话");
            return
        }
        // 弹窗询问
        dialog.show(
            "dialog_Special",
            "专车订单提交",
            "订单提交后，客服将在2天内与您电话联系<br/>了解更详细的包车需求<br/>请注意接听，谢谢",
            "好的",
            function(){
                console.log(
                    startName,
                    startDistrict,
                    startLng,
                    startLat,
                    endName,
                    endDistrict,
                    endLng,
                    endLat,
                    date,
                    people,
                    phone);
                // todo 提交信息

                $(location).attr('href', "page/specialSuccess.html");
            },
            "暂不提交",
            function(){
                dialog.hide("dialog_Special");
            }
        );
    },

    // 打开日历
    scheduleShow: function(){
        $(this.schedule).show();
    },

    // 关闭日历
    scheduleHide: function(){
        var _this = this;
        $(this.schedule).addClass("hide");
        setTimeout(function(){
            $(_this.schedule).removeClass("hide").hide();
        },300);
    },

    // 打开日历时间
    shijianShow: function(){
        $(this.shijian).show();
    },

    // 关闭日历时间
    shijianHide: function(){
        var _this = this;
        $(this.shijian).addClass("hide");
        setTimeout(function(){
            $(_this.shijian).removeClass("hide").hide();
        },300);
    },

    // 初始化
    init: function(){
        var _this = this;

        // 绑定站点选择、位置搜索
        $(".choose-site").bind("click",function(){
            siteList.show($(this).attr("id"));
        });
        $(".search-place").bind("click",function(){
            search.show($(this).attr("id"));
        });

        // 绑定提交按钮        
        $("#submit_pooling").bind("click",function(){
            _this.submitPooling();
        });
        $("#submit_special").bind("click",function(){
            _this.submitSpecial();
        });

        // 拼车日期赋值并绑定事件
        $("#pooling_date")
        .attr("date",datelist.today)
        .text(datelist.dateToText(datelist.today,true))
        .bind("click",function(){
            _this.scheduleShow();
        });

        // 关闭日历窗口
        $(this.schedule).find(".close").bind("click",function(){
            _this.scheduleHide();
        });

        // 专车绑定事件
        $("#special_date").bind("click",function(){
            _this.shijianShow();
        });

        // 关闭日历时间窗口
        $(this.shijian).find(".close").bind("click",function(){
            _this.shijianHide();
        });

    }
}

// 拼车日历
var indexSchedule = new Schedule({
    el: '#schedule-box',
    clickCb: function (y,m,d) {
        var month = m,
            day = d;
        if(m.toString().length==1) month = "0"+m;
        if(d.toString().length==1) day = "0"+d;
        var date = y+"-"+month+"-"+day;
        $('#pooling_date')
        .attr("date",date)
        .text(datelist.dateToText(date,true));
        carOrder.scheduleHide();
    }
});

// 专车日历时间
$("#special_date").shijian({
    alwaysShow:true,
    timeElm:$("#shijian-box")
})


carOrder.init();